from ninja import Router

from apps.commom.advertisement import AdvertisementSerializer
from apps.schemas import R
from models.choices import IndexAdType
from models.models import Advertisement
from utils.auth import AgentAuthBearer
from utils.constant import introduce_types
from utils.decorators import handle_exception
from utils.file_handler import load_json_file

agent_index_router = Router(tags=["代理首页信息"], auth=AgentAuthBearer())


@agent_index_router.get("/index/advertisement", summary="获取首页广告")
@handle_exception("获取首页广告失败")
def get_advertisement_config(request):
    ads = Advertisement.objects.filter(type=IndexAdType.AGENT_INDEX_AD)
    if not ads:
        return R.fail("获取首页广告失败")
    return R.ok(AdvertisementSerializer(ads, many=True).data)


@agent_index_router.get("/index/introduce", summary="获取首页介绍")
@handle_exception("获取介绍类型失败")
def get_water_introduce(request):
    data = load_json_file(f"conf/{introduce_types.get('agent_index')}")
    return R.ok(data=data)
